/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：RegistryToolGenerator.cs
 *  作者：Administrator
 *  日期：2025/6/10 20:16:13
 *  功能：Nothing
*************************************************************************/

using System;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

namespace CustomBuildProcess.Editor
{
    public class RegistryToolGenerator
    {
        /// <summary>
        /// 生成注册工具文件
        /// 注意的是：这里的appid要求要和Product Name保持一致
        /// </summary>
        /// <param name="targetPath"></param>
        /// <param name="lastBuildInfo"></param>
        public static void GenerateRegistryTool(string targetPath, BuildInfo lastBuildInfo)
        {
            //只有网络版才会输出这个文件
            if (lastBuildInfo.type != "Web") return;

            try
            {
                // 指定编码为 GBK (936)
                var encoding = Encoding.GetEncoding(936);

                using (var streamWrite = new StreamWriter(targetPath, false, encoding))
                {
                    streamWrite.WriteLine("@echo off");
                    streamWrite.WriteLine("cd /d %~dp0");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("@REM echo \"%cd%\"");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("echo Windows Registry Editor Version 5.00 >regist.reg");
                    streamWrite.WriteLine("echo [HKEY_CLASSES_ROOT\\" + lastBuildInfo.appID + "] >>regist.reg");
                    streamWrite.WriteLine("echo \"URL Protocol\"=\"\" >>regist.reg");
                    streamWrite.WriteLine("echo @=\"" + lastBuildInfo.appID + " Protocol\" >>regist.reg");
                    streamWrite.WriteLine("echo [HKEY_CLASSES_ROOT\\" + lastBuildInfo.appID + "\\DefaultIcon] >>regist.reg");
                    streamWrite.WriteLine("echo @=\"%cd:\\=\\\\%\\\\" + lastBuildInfo.appID + ".exe,1\" >>regist.reg");
                    streamWrite.WriteLine("echo [HKEY_CLASSES_ROOT\\" + lastBuildInfo.appID + "\\shell] >>regist.reg");
                    streamWrite.WriteLine("echo [HKEY_CLASSES_ROOT\\" + lastBuildInfo.appID + "\\shell\\open] >>regist.reg");
                    streamWrite.WriteLine("echo [HKEY_CLASSES_ROOT\\" + lastBuildInfo.appID + "\\shell\\open\\command] >>regist.reg");
                    streamWrite.WriteLine("echo @=\"\\\"%cd:\\=\\\\%\\\\" + lastBuildInfo.appID + ".exe\\\" \\\"%%1\\\"\" >>regist.reg");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("regedit /s regist.reg");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("del regist.reg");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("echo 注册成功");
                    streamWrite.WriteLine("");
                    streamWrite.WriteLine("pause");
                }

                Debug.Log($"Successfully generated registration tool to {targetPath}");
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to generated registration tool: {e.Message}");
            }
        }
    }
}